﻿
//jQuery HTML导出Excel文件(兼容IE及所有浏览器)  
function HtmlExportToExcel(tableid) {
    //$(".s_close").remove();
    var filename = "三和"; //new Date().toLocaleString(); ;
    if (getExplorer() == 'ie' || getExplorer() == undefined) {
        HtmlExportToExcelForIE(tableid, filename);
    }
    else {
        HtmlExportToExcelForEntire(tableid, filename)
    }
}
//IE浏览器导出Excel  
function HtmlExportToExcelForIE(tableid, filename) {
    try {
        var aLink = document.getElementById(tableid);
        var reg = new RegExp($('.layui-table-page').html(),"g"); //排除分页的内容
        var newStr = aLink.outerHTML.replace(reg,'');
        $('[data-off="true"]').each(function(){ //排除操作那一列
            var cReg = new RegExp($(this).html(),"g");
           newStr =  newStr.replace(cReg,'');
        });
        var winname = window.open('', '_blank', 'top=10000');
        var strHTML = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>';
             strHTML += newStr;
             strHTML += '</table></body></html>';
        winname.document.open('application/vnd.ms-excel', 'export excel');
        winname.document.writeln(strHTML);
        winname.document.execCommand('saveas', '', filename + '.xls');
        winname.close();

    } catch (e) {
        alert(e.description);
    }
}
//非IE浏览器导出Excel  
var HtmlExportToExcelForEntire = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    return function (table, name) {
        if (!table.nodeType) { table = document.getElementById(table); }
        var reg = new RegExp($('.layui-table-page').html(),"g"); //排除分页的内容
        var newStr = table.innerHTML.replace(reg,'');

        $('[data-off="true"]').each(function(){ //排除操作那一列
            var cReg = new RegExp($(this).html(),"g");
           newStr =  newStr.replace(cReg,'');
        });
        var ctx = { worksheet: name || 'Worksheet', table: newStr }
        document.getElementById("export").href = uri + base64(format(template, ctx));
        document.getElementById("export").download = name + ".xls";
        //document.getElementById("export").click();
    }
})()
 // function HtmlExportToExcelForEntire(tableid, filename){
 //     var strHTML = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>';
 //     strHTML += document.getElementById(tableid).outerHTML;
 //     strHTML += '</table></body></html>';
 //     var excelBlob = new Blob([strHTML], {type: 'application/vnd.ms-excel'});
 //     var aLink = document.getElementById("export");
 //     aLink.href = URL.createObjectURL(excelBlob);
 //     aLink.download = filename + '.xls';
 //    

 // }
function getExplorer() {
    var explorer = window.navigator.userAgent;
    //ie   
    if (explorer.indexOf("MSIE") >= 0) {
        return 'ie';
    }
        //firefox   
    else if (explorer.indexOf("Firefox") >= 0) {
        return 'Firefox';
    }
        //Chrome  
    else if (explorer.indexOf("Chrome") >= 0) {
        return 'Chrome';
    }
        //Opera  
    else if (explorer.indexOf("Opera") >= 0) {
        return 'Opera';
    }
        //Safari  
    else if (explorer.indexOf("Safari") >= 0) {
        return 'Safari';
    }
}